import 'package:flutter/material.dart';
import '../../model/movie.dart';

class DataTableDemo extends StatefulWidget {
  DataTableDemo({Key key}) : super(key: key);

  @override
  _DataTableDemoState createState() => _DataTableDemoState();
}

class _DataTableDemoState extends State<DataTableDemo> {

  int _sortColumnIndex = 0;
  bool _sortAscending = true;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("DataTableDemo"),
        elevation: 0.0,
      ),
      body: Container(
        padding: EdgeInsets.all(16.0),
        child: ListView(
          children: [
            DataTable(
              sortColumnIndex: _sortColumnIndex, // 表格排序的列的索引
              sortAscending: _sortAscending, // true 表示升序排列
              // onSelectAll: (bool value) { }, // 修改全选的状态
              columns: [
                DataColumn(
                  label: Text("电视剧"),
                  onSort: (int index, bool ascending) {
                    setState(() {
                      _sortColumnIndex = index;
                      _sortAscending = ascending;
                    });

                    // 模拟排序
                    movies.shuffle();
                  }
                ),
                DataColumn(label: Text("作者")),
              ], 
              rows: movies.map(
                (movie) => DataRow(
                  selected: movie.selected,
                  onSelectChanged: (bool value) {
                    setState(() {
                      if (movie.selected != value) {
                        movie.selected = value;
                      }
                    });
                  },
                  cells: [
                    DataCell(Text(movie.title)),
                    DataCell(Text(movie.author)),
                  ]
                ),
              ).toList(),
            ),
          ],
        ),
      ),
    );
  }
}